package com.ruoyi.message.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.message.domain.Friend;
import com.ruoyi.message.service.IFriendService;
import com.ruoyi.system.api.RemoteProductService;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.OrderBy;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.message.domain.Message;
import com.ruoyi.message.service.IMessageService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 消息记录Controller
 *
 * @author ruoyi
 * @date 2023-10-12
 */
@RestController
@RequestMapping("/message")
public class MessageController extends BaseController {
    @Autowired
    private IMessageService messageService;
    @Autowired
    private RemoteUserService userService;
    @Autowired
    private RemoteProductService productService;
    @Autowired
    private IFriendService friendService;

    /**
     * 查询消息记录列表
     */
    @RequiresPermissions("message:message:list")
    @GetMapping("/list")
    public TableDataInfo list(Message message) {
        startPage();
        List<Message> list = messageService.selectMessageList(message);
        return getDataTable(list);
    }

    @GetMapping(value = "selectMessageBySenderIdAndToId")
    public List<Message> selectMessageBySenderIdAndToId(Message message) {
        return messageService.selectMessageBySenderIdAndToId(message);
    }

    //获取用户集合
    @GetMapping("/getUserById/{userId}")
    public R<SysUser> getUserById(@PathVariable("userId") String userId) {
        return userService.getUserById(userId, SecurityConstants.INNER);
    }

    //获取全部用户
    @GetMapping("/userList")
    public R<List<SysUser>> userList() {

        R<List<SysUser>> listR = userService.userList(new SysUser(), SecurityConstants.INNER);
        return listR;
    }

   //获取好友
    @GetMapping("/byUserId/{userId}")
    public  List<R<SysUser>> byUserId(@PathVariable("userId") String userId) {
        Friend friend = new Friend();
        friend.setUserId(Long.valueOf(userId));
        List<Friend> list = friendService.selectFriendList(friend);
        List<R<SysUser>> list1 = new ArrayList<>();
        for (Friend friend1 : list) {
            R<SysUser> user = userService.getUserById(String.valueOf(friend1.getFriendId()), SecurityConstants.INNER);
            list1.add(user);
        }
        return list1;
    }

    //获取订单
    @GetMapping("getOrderListByUserId/{userId}")
    public R<List<OrderBy>> getOrderListByUserId(@PathVariable("userId")String userId){
        return productService.getOrderByUserId(userId,SecurityConstants.INNER);
    }

    @PostMapping("addFriend/{userId}")
    public void addFriend(@PathVariable("userId") String userId){
        Friend friend = new Friend();
        friend.setFriendId(Long.valueOf(userId));
        friend.setUserId(SecurityUtils.getLoginUser().getUserid());
        List<Friend> friends = friendService.selectFriendList(friend);
        if (friends==null){
            friendService.insertFriend(friend);
        }
    }
    /**
     * 导出消息记录列表
     */
    @RequiresPermissions("message:message:export")
    @Log(title = "消息记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Message message) {
        List<Message> list = messageService.selectMessageList(message);
        ExcelUtil<Message> util = new ExcelUtil<Message>(Message.class);
        util.exportExcel(response, list, "消息记录数据");
    }

    /**
     * 获取消息记录详细信息
     */
    @RequiresPermissions("message:message:query")
    @GetMapping(value = "/{id}")
    public List<Message> getInfo(@PathVariable("id") String id) {
        return messageService.selectMessageById(id);
    }



    /**
     * 新增消息记录
     */
    @RequiresPermissions("message:message:add")
    @Log(title = "消息记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Message message) {
        return toAjax(messageService.insertMessage(message));
    }

    /**
     * 修改消息记录
     */
    @RequiresPermissions("message:message:edit")
    @Log(title = "消息记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Message message) {
        return toAjax(messageService.updateMessage(message));
    }

    /**
     * 删除消息记录
     */
    @RequiresPermissions("message:message:remove")
    @Log(title = "消息记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(messageService.deleteMessageByIds(ids));
    }
}
